﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.fminutes.vinciBitmap {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class BitmapMovieClip extends Sprite {

        public static const UNCOMPLETE:int = 0;
        public static const COMPLETING:int = 1;
        public static const COMPLETE:int = 2;

        private var _playToListEvent:BitmapMovieClipEvent;
        private var _inStage:Boolean = false;
        private var _transparentMouseEnabled:Boolean = true;
        private var _mcClass:Class;
        private var bitmap:Bitmap;
        public var bitmapStatus:int = 0;
        private var mcArray:Array;
        private var _timer:Timer;
        private var _currentFrame:uint = 0;
        private var $mcBitmapData:BitmapData;
        private var _outIsStop:Boolean = false;
        private var _isStop:Boolean = false;
        private var _totalFrames:int = 0;
        private var _fps:int = 24;
        private var _delayStatus:String = "play";
        private var _delayTime:int = 0;
        private var _delayGetTime:int = 0;

        public function BitmapMovieClip(dobj:*=null){
			//传入对象为空返回
            if (dobj == null){
                return;
            };
			//建立一个位图显示容器
            this.bitmap = new Bitmap();
            addChild(this.bitmap);
            this.mcArray = [];
            this._timer = new Timer((1000 / this.fps));
            this._timer.addEventListener(TimerEvent.TIMER, this.timerEventHandler);
            this._timer.start();
            if ((dobj is DisplayObject)){
				//如果为显示对象画转换为位图剪辑
                this.drawDisplayObject(dobj);
            } else {
                this.draw(dobj);
                this._mcClass = dobj;
            };
            addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            addEventListener(Event.REMOVED_FROM_STAGE, this.removeFromStage);
        }
        private function addToStageHandler(_arg1:Event):void{
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMover);
            this._inStage = true;
        }
        private function removeFromStage(_arg1:Event):void{
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMover);
            this._inStage = false;
        }
        private function onMouseMover(_arg1:MouseEvent):void{
            if (this.transparentMouseEnabled == false){
                if (this.hitTestPoint(_arg1.stageX, _arg1.stageY, true)){
                    this.mouseEnabled = true;
                    this.mouseChildren = true;
                } else {
                    this.mouseEnabled = false;
                    this.mouseChildren = false;
                };
            };
        }
        public function get transparentMouseEnabled():Boolean{
            return (this._transparentMouseEnabled);
        }
        public function set transparentMouseEnabled(_arg1:Boolean):void{
            this._transparentMouseEnabled = _arg1;
            if (_arg1){
                this.mouseEnabled = true;
                this.mouseChildren = true;
            } else {
                this.mouseEnabled = false;
                this.mouseChildren = false;
            };
        }
        private function draw(cls:Class):void{
            BitmapManager.draw(cls, this.compFn, this.frameCompFn);
        }
        private function drawDisplayObject(dobj:DisplayObject):void{
            BitmapProduced.draw(dobj, this.compFn, this.frameCompFn);
        }
		/**
		 *全部渲染完毕 
		 * 
		 * @param arr bitmapdate数据
		 * @param tFrame
		 * 
		 */
        private function compFn(arr:Array, tFrame:int):void{
            this._totalFrames = tFrame;
            this.mcArray = arr;
            this.bitmapStatus = BitmapMovieClip.COMPLETE;
            this._isStop = this._outIsStop;
            this.go();
            dispatchEvent(new BitmapMovieClipEvent(BitmapMovieClipEvent.DRAW_COMPLETE));
        }
		/**
		 *渲染完一帧 
		 * 
		 * @param arr
		 * @param frame
		 * 
		 */
        private function frameCompFn(arr:Array, frame:int):void{
            this._totalFrames = frame;
            this.mcArray = arr;
            if (this.bitmapStatus == BitmapMovieClip.UNCOMPLETE){
                this.bitmapStatus = BitmapMovieClip.COMPLETING;
            };
            this.go();
            dispatchEvent(new BitmapMovieClipEvent(BitmapMovieClipEvent.DRAW_FRAME_COMP));
        }
        private function timerEventHandler(_arg1:TimerEvent):void{
            if (this._inStage == false){
                return;
            };
            if (this._delayTime != 0){
                if ((getTimer() - this._delayGetTime) > this._delayTime){
                    if (this._delayStatus == "stop"){
                        this.stop();
                    } else {
                        this.play();
                    };
                    this._delayTime = 0;
                };
            };
            if (this._isStop){
                this.bitmap.smoothing = BitmapManager.smoothing;
                return;
            };
            this._currentFrame++;
            if (this._currentFrame >= this.mcArray.length){
                this._currentFrame = 0;
            };
            this.go();
            this.bitmap.smoothing = BitmapManager.smoothing;
        }
        private function go():void{
            var _local1:Point;
            if ((((this.mcArray == null)) || ((this.mcArray.length < 1)))){
                this._isStop = true;
            } else {
                if (this.mcArray.length == 1){
                    this._isStop = true;
                    this.$mcBitmapData = this.mcArray[0][0];
                    _local1 = this.mcArray[0][1];
                    this.bitmap.bitmapData = this.$mcBitmapData;
                    this.bitmap.x = _local1.x;
                    this.bitmap.y = _local1.y;
                } else {
                    if (this._currentFrame >= this.mcArray.length){
                        this._currentFrame = (this.mcArray.length - 1);
                    };
                    this.$mcBitmapData = this.mcArray[this._currentFrame][0];
                    _local1 = this.mcArray[this._currentFrame][1];
                    this.bitmap.bitmapData = this.$mcBitmapData;
                    this.bitmap.x = _local1.x;
                    this.bitmap.y = _local1.y;
                };
            };
            if (((((this._currentFrame + 1) == this.totalFrames)) && ((this.totalFrames > 1)))){
                dispatchEvent(new BitmapMovieClipEvent(BitmapMovieClipEvent.PLAY_TO_LAST));
            };
        }
        public function get bitmapData():BitmapData{
            return (this.bitmap.bitmapData);
        }
		/**
		 * 检测与某点的碰撞
		 * 
		 * @param x
		 * @param y
		 * @param boo
		 * @return 
		 * 
		 */
        override public function hitTestPoint(x:Number, y:Number, boo:Boolean=false):Boolean{
            var pos:Point;
            var pixel:uint;
            var rect:Rectangle;
            var bmpData:BitmapData = this.bitmapData;
            if (boo){
                if (bmpData != null){
					//判断透明度是否为0
					pos = this.bitmap.globalToLocal(new Point(x, y));
					pixel = bmpData.getPixel32(pos.x, pos.y);
					pixel = (pixel >> 24);
                    if (pixel != 0){
                        return (true);
                    };
                };
            } else {
				rect = bmpData.rect;
				pos = this.bitmap.globalToLocal(new Point(x, y));
                if (rect.containsPoint(pos)){
                    return (true);
                };
            };
            return (false);
        }
        public function get currentFrame():int{
            return ((this._currentFrame + 1));
        }
        public function get totalFrames():int{
            return (this._totalFrames);
        }
        public function get fps():int{
            return (this._fps);
        }
        public function set fps(f:int):void{
            this._fps = Math.max(f, 1);
            this._timer.delay = (1000 / f);
        }
        public function stop():void{
            this._isStop = true;
            this._outIsStop = true;
            this._delayTime = 0;
        }
        public function play():void{
            this._isStop = false;
            this._outIsStop = false;
            this._delayTime = 0;
        }
        public function gotoAndPlay(f:Object):void{
            var temp:int;
            if ((f is int)){
				temp = int(f);
                if (temp > 0){
                    this._currentFrame = (int(f) - 1);
                    this.go();
                    this.play();
                };
            };
        }
        public function gotoAndStop(f:Object):void{
            var temp:int;
            if ((f is int)){
				temp = int(f);
                if (temp > 0){
                    this._currentFrame = (temp - 1);
                    this.go();
                    this.stop();
                };
            };
        }
        public function nextFrame():void{
            this.stop();
            this._currentFrame++;
            this.go();
        }
        public function prevFrame():void{
            this.stop();
            this._currentFrame--;
            if (this._currentFrame < 0){
                this._currentFrame = 0;
            };
            this.go();
        }
        public function delayStop(value:int):void{
            this._delayStatus = "stop";
            this._delayTime = value;
            this._isStop = false;
            this._outIsStop = false;
            this._delayGetTime = getTimer();
        }
        public function delayPlay(value:int):void{
            this._delayStatus = "play";
            this._delayTime = value;
            this._isStop = true;
            this._outIsStop = true;
            this._delayGetTime = getTimer();
        }
		/**
		 *释放资源 
		 * 
		 */
        public function destroy():void{
            if (this._mcClass != null){
                BitmapManager.destroy(this._mcClass);
                this._mcClass = null;
            };
            if (this._timer != null){
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.timerEventHandler);
                this._timer = null;
            };
        }

    }
}//package com.fminutes.vinciBitmap 
